package cn.hznc.controller.service.toolAnalyze;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.controller.service.inventory.InventoryLocationService;
import cn.hznc.domain.entity.InventoryLocationEntity;
import cn.hznc.domain.entity.ToolForkInfoEntity;
import cn.hznc.domain.entity.ToolImportAnalysisEntity;
import cn.hznc.domain.request.inventory.AddOrEditLocationReq;
import cn.hznc.domain.request.inventory.InBoundOrOutBoundReloadReq;
import cn.hznc.domain.request.inventory.SingleWarehouseSafetyInventoryParam;
import cn.hznc.dto.AllotEmployeeImport;
import cn.hznc.exception.ToolLifeCycleException;
import cn.hznc.mapper.InventoryLocationMapper;
import cn.hznc.mapper.ToolForkInfoMapper;
import cn.hznc.repository.InventoryLocationRespository;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.util.StringUtil;
import com.sun.org.apache.bcel.internal.generic.I2F;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
@Service
public class ToolImportAnalysisService {
    @Resource
    private InventoryLocationService inventoryLocationService;

    @Resource
    private InventoryLocationMapper inventoryLocationMapper;

    @Resource
    private InventoryLocationRespository inventoryLocationRespository;
    @Resource
    private ToolForkInfoMapper toolForkInfoMapper;
    @Transactional
    public void importToolAnalysis(List<ToolImportAnalysisEntity> importData,Integer warehouseId,String operator) throws IllegalAccessException, ParseException {
        for (int i=0;i<importData.size();i++){
            if (StringUtil.isEmpty(importData.get(i).getWarehouseLocation())){
                importData.get(i).setWarehouseLocation(importData.get(i-1).getWarehouseLocation());
                importData.get(i).setToolNo(importData.get(i-1).getToolNo());
            }
        }
        LambdaQueryWrapper<ToolForkInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ToolForkInfoEntity::getPartno);
        queryWrapper.ne(ToolForkInfoEntity::getPartno,"");
        List<ToolForkInfoEntity> toolForkInfoEntities = toolForkInfoMapper.selectList(queryWrapper);
        List<String> partnoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(toolForkInfoEntities)){
            toolForkInfoEntities.forEach(toolForkInfoEntity -> {
                if (StringUtil.isNotEmpty(toolForkInfoEntity.getPartno())){
                    partnoList.add(toolForkInfoEntity.getPartno());
                }
            });
        }
        importData.forEach(i ->{
            if (!partnoList.contains(i.getPartNo())){
                ToolForkInfoEntity toolForkInfoEntity = new ToolForkInfoEntity();
                if (StringUtils.isNotEmpty(i.getIdnr())){
                    toolForkInfoEntity.setIdnr(i.getIdnr());
                }
                toolForkInfoEntity.setType("A001B004");
                toolForkInfoEntity.setPartno(i.getPartNo());
                toolForkInfoMapper.insert(toolForkInfoEntity);
            }

//            InBoundOrOutBoundReloadReq inBoundOrOutBoundReloadReq = new InBoundOrOutBoundReloadReq();
//            inBoundOrOutBoundReloadReq.setWarehouseId(warehouseId);
//            if (StringUtils.isNotEmpty(i.getIdnr())){
//                inBoundOrOutBoundReloadReq.setIdnr(i.getIdnr());
//                inBoundOrOutBoundReloadReq.setPartType(i.getIdnr());
//            }
//            if (StringUtils.isNotEmpty(i.getPartNo()))
//                inBoundOrOutBoundReloadReq.setPartno(i.getPartNo());
//            if (StringUtils.isNotEmpty(i.getInventoryNum()))
//                inBoundOrOutBoundReloadReq.setNumber(Integer.valueOf(i.getInventoryNum()));
//            inBoundOrOutBoundReloadReq.setOperator(operator);
            String warehouseLocation = i.getWarehouseLocation();
//            InventoryLocationEntity inventoryLocationEntity = new InventoryLocationEntity();
//            inventoryLocationEntity.setWarehouseId(warehouseId);
            try{
                Integer column = Integer.valueOf(warehouseLocation.substring(warehouseLocation.indexOf("-")+1));
                Integer row = Integer.valueOf(warehouseLocation.substring(1,warehouseLocation.indexOf("-")));
//            LambdaQueryWrapper<InventoryLocationEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(InventoryLocationEntity::getColumn,column);
////            lambdaQueryWrapper.eq(InventoryLocationEntity::getWarehouseId,warehouseId);
//            lambdaQueryWrapper.eq(InventoryLocationEntity::getRow,row);
//            lambdaQueryWrapper.select(InventoryLocationEntity::getId);
                List<InventoryLocationEntity> inventoryLocationEntityList =inventoryLocationRespository.getLocationList(warehouseId);
                inventoryLocationEntityList.forEach(inventoryLocationEntity -> {
                    if (inventoryLocationEntity.getColumn() == column&&inventoryLocationEntity.getRow() == row){
                        //inBoundOrOutBoundReloadReq.setInventoryLocationId(inventoryLocationEntity.getId());
                        //安全库存设置
                        if(StringUtils.isNotEmpty(i.getSafetyInventoryNum())){
//                        InventoryLocationEntity existLocation =  inventoryLocationRespository.selectByLocationNum(inventoryLocationEntity.getLocationNum());
                            String param = inventoryLocationEntity.getSafetyInventoryConfigureParam();
                            if(StringUtil.isEmpty(param) || "[]".equals(param)){
                                //新增一条记录
                                List<SingleWarehouseSafetyInventoryParam> params = new ArrayList<>();
                                SingleWarehouseSafetyInventoryParam singleWarehouseSafetyInventoryParam = new SingleWarehouseSafetyInventoryParam();
                                singleWarehouseSafetyInventoryParam.setPartno(i.getPartNo());
                                singleWarehouseSafetyInventoryParam.setToolNo(i.getToolNo());
                                singleWarehouseSafetyInventoryParam.setSafetyInventory(Integer.valueOf(i.getSafetyInventoryNum()));
                                params.add(singleWarehouseSafetyInventoryParam);
                                inventoryLocationEntity.setSafetyInventoryConfigureParam(JSONArray.toJSONString(params));
                            }else{
                                //修改
                                List<SingleWarehouseSafetyInventoryParam> paramList = JSONArray.parseArray(param, SingleWarehouseSafetyInventoryParam.class);
                                boolean flag = false;
                                for (int i1 = 0; i1 < paramList.size(); i1++) {
                                    SingleWarehouseSafetyInventoryParam existParams = paramList.get(i1);
                                    if(StringUtil.isNotEmpty(existParams.getPartno()) && existParams.getPartno().equals(i.getPartNo())){
                                        //修改类型安全库存
                                        existParams.setSafetyInventory(Integer.valueOf(i.getSafetyInventoryNum()));
                                        flag = true;
                                    }
                                }
                                if(!flag){
                                    SingleWarehouseSafetyInventoryParam singleWarehouseSafetyInventoryParam = new SingleWarehouseSafetyInventoryParam();
                                    singleWarehouseSafetyInventoryParam.setPartno(i.getPartNo());
                                    singleWarehouseSafetyInventoryParam.setToolNo(i.getToolNo());
                                    singleWarehouseSafetyInventoryParam.setSafetyInventory(Integer.valueOf(i.getSafetyInventoryNum()));
                                    paramList.add(singleWarehouseSafetyInventoryParam);
                                    inventoryLocationEntity.setSafetyInventoryConfigureParam(JSONArray.toJSONString(paramList));
                                }
                            }
                            inventoryLocationRespository.updateById(inventoryLocationEntity);
                        }
                    }
                });
            }catch (Exception e){

            }
//                try {
//                    inventoryLocationService.inboundLocationTool(inBoundOrOutBoundReloadReq);
//                } catch (ToolLifeCycleException e) {
//                    e.printStackTrace();
//                }

//            }

        });
    }
}
